﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TS.Bussiness;
using TS.Commons;
using TS.Entities;

namespace Tranco.Views
{
    public partial class UpdateSchedule : System.Web.UI.Page
    {
        private string _userId;
        private string _errorPage = "~/Error.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _userId = Request.QueryString["sid"];
                HiddenFieldId.Value = Request.QueryString["sid"];
                HiddenFieldDay.Value = Request.QueryString["day"];
                HiddenFieldWeek.Value = Request.QueryString["week"];
                HiddenFieldMonth.Value = Request.QueryString["month"];
                HiddenFieldYear.Value = Request.QueryString["year"];
                HiddenFieldSearch.Value = Request.QueryString["str"];

                GetAllGateway();
                GetAllOwner();
                GetAllNumberBike();

                LoadSchedule();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblStatus.ForeColor = Color.Red;
            lblStatus.Text = "";

            Schedule sc = new Schedule();
            sc.ScheduleId = HiddenFieldId.Value.ToNumeric();
            sc.GatewayId = drGateway.SelectedItem.Value.ToNumeric();
            sc.OwnerId = drOwner.SelectedItem.Value.ToNumeric();
            sc.NumberBikeId = drNumberBike.SelectedItem.Value.ToNumeric();
            sc.Status = drStatus.SelectedItem.Value.ToStringOrDefault();
            
            sc.NumberCont = txtNumberCont.Text.ToUpper().ToStringOrDefault();

            sc.Content = txtDetails.Text;

            int start = txtStartDate.Text.ToDateKey();
            if (start >= 0)
                sc.StartDate = start;
            else
            {
                lblStatus.Text = "Sai định dạng ngày tháng.";
                return;
            }

            int end = txtEndDate.Text.ToDateKey();
            if (end >= 0)
                sc.EndDate = end;
            else
            {
                lblStatus.Text = "Sai định dạng ngày tháng.";
                return;
            }

            if (sc.EndDate > 0 && sc.EndDate < sc.StartDate)
            {
                lblStatus.Text = "Ngày về không được nhỏ hơn ngày đi.";
                return;
            }


            var bu = new ScheduleBussiness();

            var result = bu.UpdateSchedule(sc);

            if (result > 0)
            {
                lblStatus.ForeColor = Color.Blue;
                lblStatus.Text = "Cập nhật lịch trình thành công.";
                ClientConfirm();
            }
            else
            {
                lblStatus.Text = "Không sửa được lịch trình.";
                if (result == -2)
                {
                    lblStatus.Text = "Lịch trình đã tồn tại";
                }
            }
        }


        private void GetAllGateway()
        {
            GatewayBussiness bs = new GatewayBussiness();
            drGateway.DataSource = bs.GetAllGateway();
            drGateway.DataTextField = "Name";
            drGateway.DataValueField = "GatewayId";
            drGateway.DataBind();
        }

        private void GetAllOwner()
        {
            OwnerBussiness bs = new OwnerBussiness();
            drOwner.DataSource = bs.GetAllOwner();
            drOwner.DataTextField = "Name";
            drOwner.DataValueField = "OwnerId";
            drOwner.DataBind();
        }


        private void GetAllNumberBike()
        {
            NumberBikeBussiness bs = new NumberBikeBussiness();
            drNumberBike.DataSource = bs.GetAllNumberBike();
            drNumberBike.DataTextField = "NumberPlate";
            drNumberBike.DataValueField = "NumberBikeId";

            drNumberBike.DataBind();
        }

        private void LoadSchedule()
        {
            try
            {


                ScheduleBussiness bu = new ScheduleBussiness();
                Schedule obj = bu.GetScheduleById(HiddenFieldId.Value.ToNumeric());
                if ( obj == null) return;
                drOwner.Items.FindByValue(obj.OwnerId.ToStringOrDefault()).Selected = true;
                drGateway.Items.FindByValue(obj.GatewayId.ToStringOrDefault()).Selected = true;
                drNumberBike.Items.FindByValue(obj.NumberBikeId.ToStringOrDefault()).Selected = true;
                drStatus.Items.FindByValue(obj.Status.ToStringOrDefault()).Selected = true;

                txtDetails.Text = obj.Content;
                txtStartDate.Text = obj.StartDateS;
                txtEndDate.Text = obj.EndDateS;
                txtNumberCont.Text = obj.NumberCont.ToStringOrDefault();
            }
            catch
            {

            }
        }


        public void ClientConfirm()
        {
            var strScript = new StringBuilder();
            strScript.Append("<script language=JavaScript>");
            string scriptString = string.Format(CultureInfo.CurrentCulture, 
                                        @"var result = window.confirm('Cập nhập thông tin thành công. Bạn có muốn trở lại trang quản lý?'); " +
                     "if(result) location.href='/Views/ScheduleManagement.aspx?day={0}&week={1}&month={2}&year={3}&str={4}';</script>"
                                                        ,HiddenFieldDay.Value.ToStringOrDefault()
                                                        ,HiddenFieldWeek.Value.ToStringOrDefault()
                                                        ,HiddenFieldMonth.Value.ToStringOrDefault()
                                                        ,HiddenFieldYear.Value.ToStringOrDefault()
                                                        ,HiddenFieldSearch.Value.ToStringOrDefault()
                                                                    );
            strScript.Append(scriptString);
            ClientScript.RegisterStartupScript(GetType(), "Pop", strScript.ToString());
        }
    }
}